package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import javax.naming.NamingException;

import com.domain.TreatmentTransaction;

public class TreatmentTransactionDAO extends BaseDAO {
	
	public TreatmentTransaction getTreatmentTransactionById(int transactionId, String action) {
		TreatmentTransaction t = null;
		try {
			Connection conn = getConnection();
			String sql = "select * from treatmenttransaction where transactionid=? and action=?";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setInt(1, transactionId);
			stmt.setString(2, action);
			ResultSet rs = stmt.executeQuery();
			rs.next();
			if (rs != null) {
				t = new TreatmentTransaction();
				t.setEmployeeId(rs.getString("employeeId"));
				t.setTreatmentCode(rs.getString("treatmentCode"));
				t.setTreatmentVersion(rs.getInt("treatmentVersion"));
				t.setTransactionDate(rs.getDate("transactionDate"));
				t.setAction(rs.getString("action"));
				t.setPrice(rs.getFloat("price"));
				t.setSpecialOffer(rs.getBoolean("specialoffer"));
				t.setTransactionId(rs.getInt("transactionid"));
			}
			conn.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return t;
	}
	public TreatmentTransaction getTreatmentTransactionByEmployeeId(String employeeId) {
		TreatmentTransaction t = null;
		try {
			Connection conn = getConnection();
			String sql = "select * from treatmenttransaction where employeeId=?";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, employeeId);
			ResultSet rs = stmt.executeQuery();
			rs.next();
			if (rs != null) {
				t = new TreatmentTransaction();
				t.setEmployeeId(rs.getString("employeeId"));
				t.setTreatmentCode(rs.getString("treatmentId"));
				t.setTreatmentVersion(rs.getInt("treatmentVersion"));
				t.setTransactionDate(rs.getDate("transactionDate"));
				t.setAction(rs.getString("action"));
				t.setPrice(rs.getFloat("price"));
				t.setSpecialOffer(rs.getBoolean("specialoffer"));
				t.setTransactionId(rs.getInt("transactionid"));
			}
			conn.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return t;
	}
	public int insertTransaction(TreatmentTransaction t) {
		int result = 0;
		try {
			Connection conn = getConnection();
			String sql = "insert into treatmenttransaction(employeeid,treatmentCode,treatmentversion,transactiondate,action,price,specialoffer,transactionid) values(?, ?, ?, ?, ?, ?, ?, ?)";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, t.getEmployeeId());
			stmt.setString(2, t.getTreatmentCode());
			stmt.setInt(3, t.getTreatmentVersion());
			stmt.setDate(4, t.getTransactionDate());
			stmt.setString(5, t.getAction());
			stmt.setString(6, t.getPrice()+"");
			stmt.setBoolean(7, t.isSpecialOffer());
			stmt.setString(8, t.getTransactionId()+"");
			result = stmt.executeUpdate();
			conn.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	public List<TreatmentTransaction> searchBySQL(String query) {
		List<TreatmentTransaction> list = new LinkedList<TreatmentTransaction>();
		try {
			Connection conn = getConnection();
			String sql = "select * from treatmenttransaction " + query;
			System.out.println("sql:" + sql);
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()) {
				TreatmentTransaction t = new TreatmentTransaction();
				t.setEmployeeId(rs.getString("employeeId"));
				t.setTreatmentCode(rs.getString("treatmentcode"));
				t.setTreatmentVersion(rs.getInt("treatmentVersion"));
				t.setTransactionDate(rs.getDate("transactionDate"));
				t.setAction(rs.getString("action"));
				t.setPrice(rs.getFloat("price"));
				t.setSpecialOffer(rs.getBoolean("specialoffer"));
				t.setTransactionId(rs.getInt("transactionid"));
				list.add(t);
			}
			conn.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
}
